import java.util.Map;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2021-11-17
 * Time:15:53
 */
class ListNode{
    int val;
    ListNode prev;
    ListNode next;

    public ListNode(int val){
        this.val = val;
    }
}

class MyLinkdList{
    ListNode head;
    ListNode last;

    public void display(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    public boolean contains(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head==null){
            head=node;
            last=node;
        }else{
            head.prev=node;
            node.next=head;
            head=node;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head==null){
            head=node;
            last=node;
        }else {
            last.next=node;
            node.prev=last;
            last=node;
        }
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
//                if(cur==head&&cur==last){
//                    head=null;
//                    last=null;
//                    return;
//                }
                if(cur==head){
                    head=head.next;
                    if(head.next!=null){
                        head.prev=null;
                    }else {
                        last=null;
                    }

                }else {
                    cur.prev.next=cur.next;
                    if(cur==last){
                        last=last.prev;
                    }else {
                        cur.next.prev=cur.prev;
                    }
                }
                return;//这里必须用return
            }
            cur=cur.next;
        }
    }

    //删除所有关键字为key的节点
    public void removeAll(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head.next!=null){
                        head.prev=null;
                    }else {
                        last=null;
                    }

                }else {
                    cur.prev.next=cur.next;
                    if(cur==last){
                        last=last.prev;
                    }else {
                        cur.next.prev=cur.prev;
                    }
                }
                //return;//这里必须用return
            }
            cur=cur.next;
        }
    }

    public ListNode search(int index){
        if(index<0||index>this.size()){
            return null;
        }else {
            ListNode cur = head;
            while(index!=0){
                cur=cur.next;
                index--;
            }
            return cur;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int key){
        ListNode cur = search(index);
        if(index<0||index>this.size()){
            System.out.println("index位置不合法！");
            return false;
        }
        ListNode node = new ListNode(key);
        if(cur==head){//头插
            node.next=cur;
            head=node;
        }else {
            if(cur!=null){//中间
                node.prev=cur.prev;
                cur.prev.next=node;
                node.next=cur;
                cur.prev=node;
            }else {//尾插
                node.prev=last;
                last.next=node;
                last=node;
            }
        }
        return true;
    }

//    博哥的版本
    public void clear(){
        while(head!=null){
            ListNode curNext = head.next;
            head.prev=null;
            head.next=null;
            head=curNext;
        }
        last=null;
    }


//    public void clear(){
//        if(head!=null){
//            ListNode cur = head;
//            ListNode curNext = cur.next;
//            while (curNext!=null){
//                cur.prev=null;
//                cur.next=null;
//                cur=curNext;
//                curNext=curNext.next;
//            }
//            head=null;
//            last=null;
//        }
//    }
}

public class TestDemo {
    public static void main(String[] args) {
        MyLinkdList myLinkdList = new MyLinkdList();
        myLinkdList.addFirst(1);
        myLinkdList.addFirst(1);
        myLinkdList.addFirst(1);
        myLinkdList.display();
        myLinkdList.addLast(11);
        myLinkdList.addLast(11);
        myLinkdList.addLast(11);
        myLinkdList.addLast(12);
        myLinkdList.addLast(12);
        myLinkdList.addLast(12);
        myLinkdList.display();
        myLinkdList.removeAll(1);
        myLinkdList.display();
        myLinkdList.addIndex(6,1);
        myLinkdList.display();
        myLinkdList.clear();
        myLinkdList.display();
    }

}
